package org.example.everyday;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/reverse-prefix-of-word/
 */
public class ReversePrefix {

    public static void main(String[] args) {
        ReversePrefix r  = new ReversePrefix();
         String word = "abcd";
         char ch = 'z';
         String result =  r.reversePrefix(word,ch);
         System.out.println(result);
    }

    public String reversePrefix(String word, char ch) {
        char[] array = word.toCharArray();
        Stack<Character> stack = new Stack<Character>();
        int i = 0;
        boolean isFind = false;
        for (char c:array){
            stack.push(c);
            i++;
            if(ch == c){
                isFind = true;
                break;
            }
        }
        if(!isFind){
            return word;
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()){
            sb.append(stack.pop());
        }
        sb.append(word.substring(i));
        return sb.toString();
    }

}
